package xyz.jcat.webflux.sucurity;

import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.server.ServerAuthenticationEntryPoint;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import xyz.jcat.common.web.Resp;
import xyz.jcat.common.web.RespCodeMsg;

import java.nio.file.AccessDeniedException;

public class DefaultServerAuthenticationEntryPoint implements ServerAuthenticationEntryPoint {

    @Override
    public Mono<Void> commence(ServerWebExchange serverWebExchange, AuthenticationException e) {
        if(e.getCause() instanceof AccessDeniedException) {
            return ServerUtils.writeJson(serverWebExchange, HttpStatus.FORBIDDEN, Resp.fail(RespCodeMsg.FORBIDDEN));
        }
        return ServerUtils.writeJson(serverWebExchange, HttpStatus.UNAUTHORIZED, Resp.fail(RespCodeMsg.UNAUTHORIZED));
    }
}
